package com.bsoft.controller.JSON;

import com.bsoft.service.TreatSystemService;
import com.bsoft.utils.JSONUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 金堂第一人民医院:治疗管理系统接口文档 康复
 */
@Controller
@RequestMapping(value = "JSON/Treat")
public class TreatSystemController {
    private static Logger logger = LoggerFactory.getLogger(TreatSystemController.class);

    @Autowired
    private TreatSystemService treatSystemService;

    /**=========================================================金堂人民医院=============================================================*/

    /**
     * 1 患者信息获取接口
     */
    @RequestMapping(value = "get_Treat_PatientInfo", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String get_Treat_PatientInfo(@RequestBody Map<String, Object> req) throws Exception {
        logger.info("治疗系统:获取病人信息接口:入参:" + JSONUtil.map2Json(req));
        String res = "";
        try {
            res = treatSystemService.get_Treat_PatientInfo(req);
        } catch (Exception e) {
            res = ecgHandleException(e);
        }

        logger.info("治疗系统:获取病人信息接口:出参:" + res);
        return res;
    }


    /**
     * 2 医嘱信息获取接口 医嘱计划
     */
    @RequestMapping(value = "get_Treat_Orders", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String get_Treat_Orders(@RequestBody Map<String, Object> req) throws Exception {
        String s = JSONUtil.map2Json(req);
        logger.info("治疗系统:医嘱信息获取接口:入参:" + (s.length() > 200 ? s.substring(0, 200) : s));
        String res = "";
        try {
            res = treatSystemService.get_Treat_Orders(req);
        } catch (Exception e) {
            res =ecgHandleException(e);
        }
        logger.info("治疗系统:医嘱信息获取接口:出参:" + res);
        return res;
    }


    /**
     * 3 治疗医嘱计费
     */
    @RequestMapping(value = "Treat_OrdersBill", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String Treat_OrdersBill(@RequestBody Map<String, Object> req) throws Exception {
        logger.info("治疗系统:治疗医嘱计费:入参:" +JSONUtil.map2Json(req));
        String res = "";
        try {
            res = treatSystemService.Treat_OrdersBill(req);
        } catch (Exception e) {
            res =ecgHandleException(e);
        }
        logger.info("治疗系统:治疗医嘱计费:出参:" + res);
        return res;
    }

    /**
     * 4 高风险治疗预警接口（CDSS）
     */
    @RequestMapping(value = "treatHighRiskAlert", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String treatHighRiskAlert(@RequestBody Map<String, Object> req) throws Exception {
        logger.info("治疗系统:高风险治疗预警接口:入参:" +JSONUtil.map2Json(req));
        String res = "";
        try {
            res = treatSystemService.treatHighRiskAlert(req);
        } catch (Exception e) {
            res =ecgHandleException(e);
        }
        logger.info("治疗系统:高风险治疗预警接口:出参:" + res);
        return res;
    }

    /**
     * 5 检查报告获取
     */
    @RequestMapping(value = "get_Treat_LisReport", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String get_Treat_LisReport(@RequestBody Map<String, Object> req) throws Exception {
        logger.info("治疗系统:检验报告获取:入参:" + JSONUtil.map2Json(req));
        String res = "";
        try {
            res = treatSystemService.get_Treat_LisReport(req);
        } catch (Exception e) {
            res =ecgHandleException(e);
        }
        logger.info("治疗系统:检验报告获取:出参:" + res);
        return res;
    }

    /**
     * 6 检验报告获取
     */
    @RequestMapping(value = "get_Treat_PacsReport", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String get_Treat_PacsReport(@RequestBody Map<String, Object> req) throws Exception {
        logger.info("治疗系统:检验报告获取:入参:" +JSONUtil.map2Json(req));
        String res = "";
        try {
            res = treatSystemService.get_Treat_PacsReport(req);
        } catch (Exception e) {
            res =ecgHandleException(e);
        }
        logger.info("治疗系统:检验报告获取:出参:" + res);
        return res;
    }


    public String ecgHandleException(Exception e) {
        logger.error("金堂人民医院:治疗系统:异常:", e);
        JSONObject json = new JSONObject();
        json.put("data", null);
        json.put("code", 99);
        json.put("msg", "系统错误:" + e.getMessage());
        logger.info("金堂人民医院:治疗系统:异常:出参:" + json.toString());
        return json.toString();
    }

    /**=========================================================金堂人民医院=============================================================*/

}
